var ordersApp=angular.module("ordersApp",[])
    .controller("historyOrdersController",function ($scope,$http,merchantService,$rootScope,newOrders) {
        $rootScope.active = 'ordersList';
        newOrders.newOrders();
        

        merchantService.merchantInfo(function () {
            if(!$rootScope.permission){
                window.location = 'login.html'
            }
            var req={
                method:"GET",
                url:"orders/loadHistoryOrders"
            };
            $http(req)
                .then(function (res) {
                    $scope.order=res.data.order;
                });
        });


})
    .controller("newOrdersController",function ($scope,$http,merchantService,$rootScope,newOrders) {
        $rootScope.active = 'newOrders';
        newOrders.newOrders();

        merchantService.merchantInfo(function () {
            if(!$rootScope.permission){
                window.location = 'login.html'
            }
            var req={
                method:"GET",
                url:"orders/loadNewOrders"
            };
            $http(req)
                .then(function (res) {
                    $scope.order=res.data.order;
                });
        });


        $scope.audit=function (index,status) {
            var req = {
                method: "PUT",
                url: "orders/" +$scope.order[index]._id,
                data: {status:status},
                headers: {"Content-type": "application/json"}
            };
            $http(req).then(function (res) {
                if(!res.data.error){
                    var req={
                        method:"GET",
                        url:"orders/loadNewOrders"
                    };
                    $http(req)
                        .then(function (res) {
                            $scope.order=res.data.order;
                        });
                }
            });
        }

    })
    .controller("ordersDeatilsController",function ($scope,$http,$location,merchantService,$rootScope,newOrders) {
        $rootScope.active = 'ordersList';
        newOrders.newOrders();
        

        merchantService.merchantInfo(function () {

            if(!$rootScope.permission){
                window.location = 'login.html'
            }
            
            var oid = $location.search().oid;
            var req={
                method:"GET",
                url:"orders/"+oid
            }
            $http(req).then(function (res) {
                if(!res.data.error){
                    $scope.orderItem=res.data.orderItem[0].orderItem;
                }
            });
        });



    })
    .controller("complainController",function ($scope,$http,merchantService,$rootScope,newOrders) {
        $rootScope.active = 'myComplain';
        newOrders.newOrders();
        merchantService.merchantInfo(function () {
            if(!$rootScope.permission){
                window.location = 'login.html'
            }
            var req={
                method:"GET",
                url:"orders/complain/loadComplain"
            };
            $http(req)
                .then(function (res) {
                    $scope.order=res.data.order;
                });
        });


    })
    .filter('convertConfirm', function () {
        return function (status) {
            switch (status) {
                case 0 :
                    return 'Not Confirm';
                case 1 :
                    return 'Confirm';
            }
        }
    })
    .filter('convertStatus', function () {
        return function (status) {
            switch (status) {
                case 0 :
                    return 'Wait for receive';
                case 1 :
                    return 'Receive';
                case 2 :
                    return 'Not receive';
            }
        }
    })
    .filter('convertRes', function () {
        return function (status) {
            switch (status) {
                case 0 :
                    return 'Unhandled';
                case 1 :
                    return 'Dismiss Complaint';
                case 2 :
                    return 'Deduct Deposit';
                case 3 :
                    return 'Blacklisted';
            }
        }
    });



